package com.xoa.controller.party;

import com.xoa.model.party.Archives;
import com.xoa.model.party.ArchivesExample;
import com.xoa.model.party.ArchivesWithBLOBs;
import com.xoa.service.party.ArchivesService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qiyang on 2019/4/30.
 */
@Controller
@RequestMapping("/street")
public class ArchivesController {

    @Autowired
    private ArchivesService archivesService;

    /**
     * 映射
     * @return
     */

    @RequestMapping("/mainPage")
    public String mainPage() {

        return "app/party/archivesIndex";
    }
    @RequestMapping("/archivesStraff")
    public String mainPage1() {

        return "app/party/archivesStraff";
    }
    @RequestMapping("/archivesNew")
    public String mainPage2() {

        return "app/party/archivesNew";
    }
    @RequestMapping("/archivesQuery")
    public String mainPage3() {

        return "app/party/archivesQuery";
    }

    /**
     * 查询
     * @param example
     * @return
     */
    @ResponseBody
    @GetMapping("/search")
    public List<ArchivesWithBLOBs> findAll(ArchivesExample example){
        List<ArchivesWithBLOBs> list = archivesService.findAll(example);
        System.err.println("====================="+list);
        return list;
    }

    @ResponseBody
    @GetMapping("/findAll")
    public ToJson<ArchivesWithBLOBs> selectByCondition(HttpServletRequest request, Integer page, Integer pageSize, Boolean useFlag, ArchivesWithBLOBs archivesWithBLOBs){
        Map<String,Object> map= new HashMap<String, Object>();
        map.put("obj",archivesWithBLOBs);
        ToJson json =new ToJson<ArchivesWithBLOBs>(1,"err");
        try {
            List<ArchivesWithBLOBs> data =archivesService.findAll(page, pageSize, useFlag, map);
            if(data!=null){
                PageParams pageParams = (PageParams) map.get("page");
                json.setTotleNum(pageParams.getTotal());
                json.setObj(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return json;
    }

    /**
     * 添加
     */
    @ResponseBody
    @PostMapping("/insert")
    public ToJson<Archives> insert(HttpServletRequest request , ArchivesWithBLOBs archives){
        return archivesService.insert(request,archives);

    }

    /**
     * 修改
     * @param archives
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public ToJson<Archives> update(HttpServletRequest request , ArchivesWithBLOBs archives){
        return archivesService.update(request,archives);

    }

    /**
     * 根据id回显
     * @param archivesId
     * @return
     */
    @ResponseBody
    @PostMapping("/selectByPrimaryKey")
    public Archives selectByPrimaryKey(Integer archivesId){
        System.err.println("==================="+archivesId);
        return archivesService.selectByPrimaryKey(archivesId);
    }

    /**
     * 删除
     * @param archivesId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ToJson<Archives> delete(HttpServletRequest request , Integer archivesId){
        return archivesService.delete(request , archivesId);
    }

}
